// component/floatWindow/index.js
var app = getApp();
import Toast from "../../vant/weapp/toast/toast";

const util = require("../../utils/util");

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    latitude: Number,
    longitude: Number,
  },

  /**
   * 组件的初始数据
   */
  data: {
    rightArr: [{
      door_list: [{
        door_name: "开门"
      }],
    }],
    add: [],
    addIndex: 0,
    show: false,
    timestart: "",
    timeEnd: "",
  },
  properties: {
    door: {
      type: Object,
      value: {},
      observer(e) {
        console.log(99, e)
        this.setData({
          rightArr: e
        })

      }
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    opendoor(e) {
      let that = this;
      // order:订单数量，0为没有，显示暂无预约，1为一个，直接开门，2及2以上跳转页面，自行选择开门订单

      if (wx.getStorageSync('token') === "") {
        Toast.fail("请先登录")
        return
      }

      console.log(that.data.rightArr)
      if (that.data.rightArr == "") {
        Toast.fail("暂无预约订单")
        return
      }
      // 只有一个订单
      if (that.data.rightArr.length === 1) {
        let i = that.data.rightArr[0]
        that.open2(i)
      } else {
        wx.reLaunch({
          url: '/pages/order/index',
        })
      }


    },
    //加时
    extend() {
      const that = this
      if (wx.getStorageSync('token') == "") {
        that.triggerEvent("is_user_show", false);
        return
      }
      if (wx.getStorageSync('token') == "") {
        that.triggerEvent("is_user_show", false);
        return
      }
      if (that.data.rightArr == "") {
        Toast.fail("暂无预约订单")
        return
      }
      if (that.data.rightArr.order === 1) {
        if (that.data.rightArr.order_type !== 2) {
          Toast.fail("该订单不可延长")
        } else if (that.data.rightArr.order_type === 2) {
          that.getRenewal()
          that.setData({
            show: true,
          });
        }
      } else {
        wx.reLaunch({
          url: '/pages/order/index',
        })
      }
    },
    // 续单
    getRenewal() {
      app.http.post(app.api.orderRenewal, {
        order_on: this.data.rightArr.order_on
      }, (res) => {
        this.setData({
          add: res.data
        })
        this.getTime()
      })
    },

    // 包厢法门
    open2(i) {
      console.log('包厢法门')
      console.log(i)
      const that = this
      app.http.post(app.api.DoorIndex, {
        order_on: i.order_on
      }, res => {
        if (res.code === 200) {
          Toast.success("开门成功")
        } else {
          Toast.fail(res.message)
        }

      })

    },
    bindAddChange(e) {
      const that = this;
      that.getTime()
      that.setData({
        addIndex: e.detail.value,
      });
    },
    getTime() {
      const that = this
      let start = that.data.add.order.end_time * 1000
      let timestart = util.formatHS(start)
      // let timeEnd=
      let next_time = new Date(timestart.replace(/\-/g, "/")).setHours(
        new Date(timestart.replace(/\-/g, "/")).getHours() + that.data.add.renewal[that.data.addIndex].time
      );
      let timeEnd = util.formatH(next_time);
      that.setData({
        timestart,
        timeEnd
      })
    },
    addTime() {
      const that = this;
      app.http.post(app.api.renewalOrder, {
        order_on: that.data.rightArr.order_on,
        happy_id: that.data.add.renewal[that.data.addIndex].happy_id
      }, (res) => {
        console.log(res.data.order_on)
        wx.navigateTo({
          url: '/pages/index/order/index?order_on=' + res.data.order_on + "&from=renew",
        })
        that.setData({
          show: false
        })
      })
    },
    onClose(e) {
      this.setData({
        show: false,
      });
    },
  },
});